import echarts from "echarts"
import { toRad } from "../utils";

/**
 * 将小圆形添加到图表上
 * @param {*} chart 
 * @param {*} r 
 * @param {*} r1 
 * @param {*} angle 
 * @param {*} animate 
 * @param {*} fill 
 */

export function addCircleToChart(chart, r, r1, angle = 0, animate = true, fill = '#03e7fc') {
  
  let zr = chart.getZr();
  let cx = zr.getWidth() / 2;
  let cy = zr.getHeight() / 2;

  let x = cx + r * Math.cos(toRad(angle));
  let y = cy + r * Math.sin(toRad(angle));

  let circle = new echarts.graphic.Circle({
    origin: [cx, cy],
    shape: {
      cx: x,
      cy: y,
      r: r1
    },
    style: {
      fill: fill,
    },
    zlevel: 2
  });

  if(animate){
    animateRotate(circle);
  }
  zr.add(circle);
  return circle;
}

/**
 * 为元素添加旋转动画
 * @param {*} element 元素
 * @param {*} clockwise 是否顺时针
 */
export function animateRotate(element, clockwise = true) {
  let rotation = clockwise ? -2 * Math.PI : 2 * Math.PI
  function animateLoop() {
    element.animateTo({ rotation: rotation }, 10000, function () {
      element.attr({
        rotation: 0
      });
      setTimeout(animateLoop, 1);
    });
  }

  animateLoop();
}
